home *** CD-ROM | disk | FTP | other *** search
- Path: dd.chalmers.se!news.chalmers.se!sunic!pipex!howland.reston.ans.net!agate!ames!olivea!hal.com!parlo.hal.COM!not-for-mail
- From: paul@hal.COM (Paul Sander)
- Newsgroups: comp.lang.c,comp.std.c
- Subject: Re: allocating 0 bytes with malloc()?
- Date: 27 Feb 1994 02:37:46 -0800
- Organization: HaL Computer Systems, Inc.
- Lines: 46
- Message-ID: <2kpt5q$6gb@parlo.hal.COM>
- References: <1994Feb21.112831.1594@inmos.co.uk> <mmj5ohINNphv@exodus.eng.sun.com> <id.DZ871.EL3@nmti.com> <mn092vINNa0o@exodus.Eng.Sun.COM>
- NNTP-Posting-Host: parlo.hal.com
- Xref: dd.chalmers.se comp.lang.c:10209 comp.std.c:1104
-
- In article <mn092vINNa0o@exodus.Eng.Sun.COM>,
- Robert Corbett <corbett@lupa.Eng.Sun.COM> wrote:
- >>In article <mmj5ohINNphv@exodus.eng.sun.com>,
- >>
- >>Non-portable code:
- >>
- >> ptr = malloc(size);
- >> if(ptr==0 && size!=0) fail...
- >>
- >>Portable code:
- >>
- >> if(size == 0)
- >> ptr = NULL;
- >> else {
- >> ptr = malloc(size);
- >> if(ptr==0) fail...
- >> }
- >>
- >>I don't see the problem.
- >
- >Merriam Webster's Collegiate Dictionary, tenth edition, defines the word
- >obstacle as "something that impedes progress or achievement." Note the
- >word "impedes." An obstacle need not prevent progress or achievement, it
- >need only impede it.
-
- Most folks I know who are concerned about this issue have this macro in
- their portability library:
-
- #define myMalloc(x) ( (x) ? malloc(x) : (void*) 0 )
-
-
- They don't seem to consider malloc's behavior in this regard to be much
- of an obstacle. And those that don't like their system's implementation
- of malloc for whatever reasons replace it anyway; there are lots of free
- implementations of it around and it's easy to add one to a link line.
-
- I guess I work on a different set of problems than Mr. Corbett; I've
- never needed to pass a 0 argument to malloc. Instead, I've always found
- ways to optimize away the need to allocate no memory. But I would be
- interested in reading (via email) about cases where this technique
- substantially simplified the code.
- --
- Paul M. Sander (408) 379-7000 | "You are in a maze of twisty little
- HaL Computer Systems, Inc. | methods, all just a little different."
- 1315 Dell Avenue |
- Campbell, CA 95008 USA |
-